﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using SWC = System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Reporting.WinForms;
using SQLServerOptimizer.PL.WPF.Datasets.IssuesReportDatasetTableAdapters;
using System.Configuration;
using SQLServerOptimizer.PL.WPF.Datasets;
using System.Windows.Forms;
using AG.Utilities;
using SQLServerHealthCheck.Model;
using SQLServerHealthCheck.SQLServer;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace SQLServerOptimizer.PL.WPF
{
    /// <summary>
    /// Interaction logic for IssuesReportControl.xaml
    /// </summary>
    public partial class AutoFixControl //: SWC.UserControl
    {
        public static int serviceId = -1, databaseId = -1,Mid=2,Low=1, expandlevel = 1;
        public static int High = 3;
        private HealthCheckDataContext dc;
        public List<Service> SQLServerInstances;
        public Service instance;
        public Database database;

        public AutoFixControl()
        {
          
            InitializeComponent();
            //DataContext = severities;
        }

      
        
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (MainWindow.IsDatabaseCreated())
               {
            FillSQLServicesCombobox();
             }
        
        }

        


        private void combobox_servicefilter_SelectionChanged(object sender, SWC.SelectionChangedEventArgs e)
        {
            try
            {


                if (this.combobox_servicefilter.SelectedIndex != -1)
                {
                    try
                    {
                        instance = (combobox_servicefilter.SelectedItem as Service);
                        var service = from ser in dc.Services
                                      where ser.ServiceID == instance.ServiceID
                                      select ser;
                        foreach (var ser in service)
                        {
                            serviceId = ser.ServiceID;
                        }
                        if (service.Count() == 0)
                        {
                            serviceId = -1;
                        }
                        if (serviceId != -1)
                        {
                            var qry = from db in dc.Databases
                                      where db.ServiceID == serviceId
                                      select db;

                            List<Database> databases = qry.ToList<Database>();
                            Database dummy = new Database { DatabaseID = -1, DatabaseName = "All" };

                            databases.Insert(0, dummy);

                            this.combobox_databasefilter.ItemsSource = databases;
                            this.combobox_databasefilter.DisplayMemberPath = "DatabaseName";

                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex);
                    }
                }

                RefreshReport();
                this.combobox_databasefilter.SelectedIndex = 0;
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private void combobox_databasefilter_SelectionChanged(object sender, SWC.SelectionChangedEventArgs e)
        {

            try
            {


                if (this.combobox_databasefilter.SelectedIndex != -1)
                {
                    try
                    {


                        if (this.combobox_databasefilter.SelectedIndex != -1)
                        {
                            database = (this.combobox_databasefilter.SelectedItem as Database);
                            databaseId = database.DatabaseID;


                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex);
                    }
                }

                RefreshReport();
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            RefreshReport();
        }

        public void FillSQLServicesCombobox()
        {
            try
            {
                string conn = System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionStringSSHCDatabase");
                CheckInstanceStatusWorker checkinstanceStatusworker = new CheckInstanceStatusWorker(this, conn);

                checkinstanceStatusworker.AccessiblityResult += new InstanceStatusResult(checkinstanceStatusworker_AccessiblityResult);
                checkinstanceStatusworker.Start();
            }
            catch (Exception sql)
            {
                Logger.Error(sql);
            }
        }

        public void RefreshReport()
        {

            try
            {

                this.data.InitialLoad(); this.data.Refresh();
                this.tree.Items.Refresh();

            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }





        }

        private void checkinstanceStatusworker_AccessiblityResult(bool accessible)
        {
            try
            {

                if (accessible)
                {
                    dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings.Get("ConnectionStringSSHCDatabase"));

                    var qry = from i in dc.Services
                              where i.ServiceTypeID == 1
                              select i;
                    SQLServerInstances = qry.ToList<Service>();
                    Server dummy = new Server { Name = "All" };
                    Service dummyService = new Service { ServiceID = -1, Server = dummy, Name = "" };

                    SQLServerInstances.Insert(0, dummyService);

                    this.combobox_servicefilter.ItemsSource = SQLServerInstances;
                    this.combobox_servicefilter.DisplayMemberPath = "InstanceName";
                    this.combobox_servicefilter.SelectedValuePath = "ServiceID";
                    var main = App.Current.MainWindow as MainWindow;
                    this.combobox_servicefilter.SelectedIndex = main.combobox_servicefilterA.SelectedIndex;
                    instance = (combobox_servicefilter.SelectedItem as Service);
                    var qry2 = from db in dc.Databases
                               where db.ServiceID == instance.ServiceID
                               select db;

                    List<Database> databases = qry2.ToList<Database>();
                    Database dummyDatabase = new Database { DatabaseID = -1, DatabaseName = "All" };

                    databases.Insert(0, dummyDatabase);


                   


                    this.combobox_databasefilter.ItemsSource = databases;
                    this.combobox_databasefilter.DisplayMemberPath = "DatabaseName";

                     // If not a static method, this.MainWindow would work
                    this.combobox_databasefilter.SelectedIndex = main.combobox_databasefilterA.SelectedIndex;

                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private void combobox_Expand_SelectionChanged(object sender, SWC.SelectionChangedEventArgs e)
        {

            expandlevel = combobox_Expand.SelectedIndex;

            RefreshReport();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            IssueViewModel a = (IssueViewModel)this.tree.Items[0];
            fixer fxr = new fixer();
            fxr.fixissues(a);
            this.data.InitialLoad(); this.data.Refresh();
            this.tree.Items.Refresh();


        }
     
        

        private void ListView_Severity_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {

            try
            {


                RefreshReport();
            }

            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }

        private void ListView_Severity_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        private void ListView_Severity_SelectionChanged(object sender, SWC.SelectionChangedEventArgs e)
        {

        }

    }
}
